package similan.br32;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "orgAutoCompleteBean")
@RequestScoped
public class OrganizationAutoCompleteBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<OrganizationAutoCompleteTag> orgList = null;

	private OrganizationAutoCompleteTag selectedTag = null;

	@PostConstruct
	public void init() {

		/**
		 * Get all organization and create Tag
		 */
		// orgList = orgService.getOrganizationAutoCompleteTags();
		System.out.println("Post init of OrganizationAutoCompleteBean");

		/* this is test code to be removed later */
		if (orgList == null) {
			orgList = new ArrayList<OrganizationAutoCompleteTag>();
			OrganizationAutoCompleteTag tag1 = new OrganizationAutoCompleteTag();
			tag1.setId(1);
			tag1.setName("CISCO");
			orgList.add(tag1);

			OrganizationAutoCompleteTag tag2 = new OrganizationAutoCompleteTag();
			tag2.setId(2);
			tag2.setName("Polycom");
			orgList.add(tag2);

			OrganizationAutoCompleteTag tag3 = new OrganizationAutoCompleteTag();
			tag3.setId(3);
			tag3.setName("General Electric");
			orgList.add(tag3);

			OrganizationAutoCompleteTag tag4 = new OrganizationAutoCompleteTag();
			tag4.setId(4);
			tag4.setName("Juniper");
			orgList.add(tag4);
		}
	}

	public OrganizationAutoCompleteTag getSelectedTag() {
		return selectedTag;
	}

	public void setSelectedTag(OrganizationAutoCompleteTag selectedTag) {
		this.selectedTag = selectedTag;
	}

	public List<String> complete(String query) {
		List<String> results = new ArrayList<String>();

		for (int i = 0; i < 10; i++) {
			results.add(query + i);
		}

		return results;
	}

	public List<OrganizationAutoCompleteTag> completeOrgTag(String query) {

		System.out.println("completeOrgTag of OrganizationAutoCompleteBean");
		List<OrganizationAutoCompleteTag> suggestions = new ArrayList<OrganizationAutoCompleteTag>();

		for (OrganizationAutoCompleteTag orgTag : orgList) {
			if (orgTag.getName().startsWith(query))
				suggestions.add(orgTag);
		}

		return suggestions;
	}

	public List<OrganizationAutoCompleteTag> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<OrganizationAutoCompleteTag> orgList) {
		this.orgList = orgList;
	}
}
